﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 编辑备注文本命令。
    /// ★①，修改类名。
    /// </summary>
    public static class EditCommentTextCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static EditCommentTextCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand("EditCommentTextCommand",
                "EditCommentTextCommand",
                typeof(EditCommentTextCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.Enter,ModifierKeys.Control,"Ctrl+Enter"),
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。
            if (Globals.MainWindow.SelectedRelation != null)
            {
                EditRelationText(); return;
            }

            if (Globals.MainWindow.SelectedComment != null)
            {
                EditComment(); return;
            }

            EditCommentTextOfTitle();
        }

        #region 已改用富文本块实现Comment的内容。2011年9月5日
        //public static void EditComment(string str = null)
        //{
        //    MainWindow mainWindow = Globals.MainWindow;
        //    Dialogs.BaseTextEditor tte;

        //    string initilizedText;

        //    if (str == null)
        //    {
        //        initilizedText = Globals.MainWindow.SelectedComment.Text;
        //        tte = new Dialogs.BaseTextEditor(true);
        //    }
        //    else
        //    {
        //        initilizedText = str;
        //        tte = new Dialogs.BaseTextEditor();
        //    }

        //    tte.WindowStartupLocation = WindowStartupLocation.Manual;
        //    tte.Width = Globals.MainWindow.mainScrollViewer.ActualWidth - 54;
        //    Point ptBase = new Point(0, 0);
        //    Point ptStart = Globals.MainWindow.mainScrollViewer.PointToScreen(ptBase);
        //    tte.Left = ptStart.X + 26;
        //    tte.Height = 100;
        //    tte.Top = Globals.MainWindow.mainScrollViewer.ActualHeight + ptStart.Y - 136;
        //    tte.Owner = Globals.MainWindow;



        //    if (initilizedText != null)
        //    {
        //        tte.inputTextBox.Text = initilizedText;
        //    }

        //    if (tte.ShowDialog() == true)
        //    {
        //        ModifingItem mi = new ModifingItem() { ModifingName = "编辑备注文本块文本" };

        //        Action actCommentText = new Action(Globals.MainWindow.SelectedComment.ID,
        //            -1, Globals.MainWindow.SelectedComment.GetType().Name, "Text",
        //            Globals.MainWindow.SelectedComment.Text, tte.inputTextBox.Text);

        //        Globals.MainWindow.SelectedComment.Text = tte.inputTextBox.Text;

        //        mi.Add(actCommentText);

        //        Globals.MainWindow.Do(mi);
        //        Globals.MainWindow.IsModified = true;

        //        Globals.MainWindow.SelectedComment.RefreshConnectLine();
        //    }
        //} 
        #endregion

        public static void EditComment(string initializeText = null)
        {
            MainWindow mainWindow = Globals.MainWindow;
            Dialogs.CommentEditor cte = new Dialogs.CommentEditor(
                Globals.MainWindow.SelectedComment, initializeText);

            var sv = Globals.MainWindow.mainScrollViewer;

            cte.WindowStartupLocation = WindowStartupLocation.Manual;
            cte.Width = sv.ActualWidth - 54;
            Point ptBase = new Point(0, 0);
            Point ptStart = sv.PointToScreen(ptBase);
            cte.Left = ptStart.X + (sv.ActualWidth - Math.Min(cte.MaxWidth, cte.Width)) / 2;
            cte.Height = 120;
            cte.Top = Globals.MainWindow.mainScrollViewer.ActualHeight + ptStart.Y - 130;
            cte.Owner = Globals.MainWindow;

            Title.TryToShowTitleInVisualArea(Globals.MainWindow.MainSelectedTitle, true);

            cte.ShowDialog();

            Globals.MainWindow.RefreshTitleAndLinesLocation();
        }

        public static void EditRelationText(string str = null)
        {
            MainWindow mainWindow = Globals.MainWindow;

            Dialogs.BaseTextEditor tte;

            string initilizedText;

            if (str == null)
            {
                initilizedText = Globals.MainWindow.SelectedRelation.CommentText;
                tte = new Dialogs.BaseTextEditor(true);
            }
            else
            {
                initilizedText = str;
                tte = new Dialogs.BaseTextEditor();
            }

            System.Windows.Controls.ScrollViewer sv = Globals.MainWindow.mainScrollViewer;

            tte.WindowStartupLocation = WindowStartupLocation.Manual;
            tte.Width = sv.ActualWidth - 54;
            Point ptBase = new Point(0, 0);
            Point ptStart = sv.PointToScreen(ptBase);
            tte.Left = ptStart.X + (sv.ActualWidth - Math.Min(tte.MaxWidth, tte.Width)) / 2;
            tte.Height = 120;
            tte.Top = sv.ActualHeight + ptStart.Y - 130;
            tte.Owner = Globals.MainWindow;
            tte.inputTextBox.Background = Globals.MainWindow.SelectedRelation.TextBackground;

            if (initilizedText != null)
            {
                tte.inputTextBox.Text = initilizedText;
                tte.inputTextBox.Select(tte.inputTextBox.Text.Length, 0);
            }

            if (tte.ShowDialog() == true)
            {
                ModifingItem mi = new ModifingItem() { ModifingName = "编辑关系线备注文本" };

                Action actCommentText = new Action(Globals.MainWindow.SelectedRelation.ID,
                    -1, Globals.MainWindow.SelectedRelation.GetType().Name, "CommentText",
                    Globals.MainWindow.SelectedRelation.CommentText, tte.inputTextBox.Text);

                Globals.MainWindow.SelectedRelation.CommentText = tte.inputTextBox.Text;

                mi.Add(actCommentText);

                Globals.MainWindow.Do(mi);
                Globals.MainWindow.IsModified = true;

                Globals.MainWindow.SelectedRelation.RefreshLocation();//重排备注文本位置。
            }
        }

        internal static void EditCommentTextOfTitle()
        {
            Title mainSelectedTitle = Globals.MainWindow.MainSelectedTitle;

            if (Globals.SelectedTitlesList.Count <= 0 || mainSelectedTitle == null) return;

            MainWindow mainWindow = Globals.MainWindow;

            Dialogs.TitleCommentEditor tce = new Dialogs.TitleCommentEditor(Globals.SelectedTitlesList);

            System.Windows.Controls.ScrollViewer sv = mainWindow.mainScrollViewer;

            tce.WindowStartupLocation = WindowStartupLocation.Manual;
            tce.Width = sv.ActualWidth - 54;
            Point ptBase = new Point(0, 0);
            Point ptStart = sv.PointToScreen(ptBase);
            tce.Left = ptStart.X + (sv.ActualWidth - Math.Min(tce.MaxWidth, tce.Width)) / 2;
            tce.Height = 120;
            tce.Top = Globals.MainWindow.mainScrollViewer.ActualHeight + ptStart.Y - 130;
            tce.Owner = Globals.MainWindow;

            Title.TryToShowTitleInVisualArea(Globals.MainWindow.MainSelectedTitle, true);

            tce.ShowDialog();

            Globals.MainWindow.RefreshTitleAndLinesLocation();
            
            #region 旧式编辑，不支持富文本。
            //Dialogs.BaseTextEditor tte = new Dialogs.BaseTextEditor();

            //tte.WindowStartupLocation = WindowStartupLocation.Manual;
            //tte.Width = Globals.MainWindow.mainScrollViewer.ActualWidth - 54;
            //Point ptBase = new Point(0, 0);
            //Point ptStart = Globals.MainWindow.mainScrollViewer.PointToScreen(ptBase);
            //tte.Left = ptStart.X + 26;
            //tte.Height = 100;
            //tte.Top = Globals.MainWindow.mainScrollViewer.ActualHeight + ptStart.Y - 136;
            //tte.Owner = Globals.MainWindow;

            //Title mainSelectedTitle = Globals.MainWindow.MainSelectedTitle;
            //if (mainSelectedTitle != null)
            //{
            //    string initilizedText = mainSelectedTitle.CommentText;
            //    if (initilizedText != null)
            //    {
            //        tte.inputTextBox.Text = initilizedText;
            //    }
            //    Title.TryToShowTitleInVisualArea(Globals.MainWindow.MainSelectedTitle, true);
            //}

            //if (tte.ShowDialog() == true)
            //{
            //    ModifingItem mi = new ModifingItem()
            //    {
            //        IsNeedRefreshTitleAndLinesLocation = true,
            //        ModifingName = "编辑标题备注文本"
            //    };

            //    foreach (Title t in Globals.SelectedTitlesList)
            //    {
            //        Action actCommentText = new Action(t.ID, -1, t.GetType().Name, "CommentText",
            //            t.CommentText, tte.inputTextBox.Text);
            //        t.CommentText = tte.inputTextBox.Text;
            //        mi.Add(actCommentText);
            //    }

            //    Globals.MainWindow.Do(mi);
            //    Globals.MainWindow.IsModified = true;
            //    Globals.MainWindow.RefreshTitleAndLinesLocation();
            //} 
            #endregion
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            if (Globals.MainWindow.SelectedRelation != null)
            {
                e.CanExecute = true; return;
            }

            if (Globals.MainWindow.SelectedComment != null)
            {
                e.CanExecute = true; return;
            }

            e.CanExecute = (Globals.SelectedTitlesList.Count > 0);
        }
    }
}
